<?php
namespace app\index\controller;

use think\Cache;
use think\Controller;

/**
 * @description : [基类控制器]
 * Class Base
 * @package app\index\controller
 */
class Base extends Controller
{

    protected $config;

    const CONFIG_CACHE_KEY = 'config_cache_key';

    public function _initialize(){
        $this->loadConfig();
        $this->assign('config', $this->config);
    }

    /**
     * @description : [默认加载配置]
     * @return array
     * @throws \think\exception\DbException
     */
    private function loadConfig(){
        $config = model('Config')->order('create_time DESC')->select();
//        if(Cache::has(self::CONFIG_CACHE_KEY)){
//            $configArr = json_decode(Cache::get(self::CONFIG_CACHE_KEY), true);
//        }else{
            $configArr = [];
            foreach ($config AS $key=>$val){
                $configArr[$val['config_key']] = $val['config_value'];
            }
            Cache::set(self::CONFIG_CACHE_KEY, json_encode($configArr, true));
//        }
        $this->config = $configArr;
    }

    /**
     * @description : [根据类型获取广告]
     * @param $type
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\exception\DbException
     */
    protected function getAdByType($type){
        if(isset($type)){
            $where['location_id'] = $type;
        }
        $banner = model('Ad')->where($where)->order('create_time DESC')->select();
        return $banner;
    }

}
